var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
//权限拦截，无登陆信息不能登陆
exports.getSessionID=function (req, res, next) {
    var session=req.session;
    return session.id;
}
exports.checkAuth=function(req, res, next) {
    var session=req.session;
    // var token = req.signedCookies.token;
    // if (token && req.session.user && req.session.user.token === token)
    //     next();
    // else if (token) {
    //     //if invalid token or no session, should rebuild
    //     var authInfo = user.getAuthInfo(token);
    //     if (authInfo && authInfo.isAuth) {
    //         req.session.user = {
    //             userID: authInfo.userID,
    //             userName: authInfo.userName,
    //             isAuth: authInfo.isAuth,
    //             token: token
    //         }
    //         next();
    //
    //     } else
    //         res.redirect('/login');
    // } else
    //     res.redirect('/login');
}
exports.addUserInfo=function(req, res, next) {
    var token = req.signedCookies.token;
    if (token && req.session.user && req.session.user.token === token)
        next();
    else if (token) {
        //if invalid token or no session, should rebuild
        var authInfo = user.getAuthInfo(token);
        if (authInfo && authInfo.isAuth) {
            req.session.user = {
                userID: authInfo.userID,
                userName: authInfo.userName,
                isAuth: authInfo.isAuth,
                token: token
            }
            next();

        } else
            res.redirect('/login');
    } else
        res.redirect('/login');
}

